/* $RCSFile: FAQsDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/FAQs/FAQsDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:54  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;


namespace AddonNice.Settings.PageModules.FAQs
{

	/// <summary>
	/// IBS Portal FAQ module
	/// </summary>
	public class FAQsDB : DBDataStore
	{

		/// <summary>
		/// The AddFAQ function is used to ADD FAQs to the DatabaseDB
		/// </summary>
		public int AddFAQs(int moduleID, string userName,string question,string answer) 
		{
			SqlCommand myCommand			= new SqlCommand("dbo.adn_AddFAQs", myConnection);
            myCommand.CommandType			= CommandType.StoredProcedure;

            SqlParameter parameterItemID		=	new SqlParameter(StStr.ATItemID, SqlDbType.Int, 4);
            parameterItemID.Direction           =    ParameterDirection.Output;
            myCommand.Parameters.Add(parameterItemID);

            AddModuleIDParameters(myCommand,moduleID);

			SqlParameter parameterUserName	= new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
			parameterUserName.Value			= userName;
			myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterQuestion	= new SqlParameter("@Question", SqlDbType.NVarChar, 400);
            parameterQuestion.Value			= (question.Length > 400) ? question.Substring(0,400):question;
            myCommand.Parameters.Add(parameterQuestion);

            SqlParameter parameterAnswer	= new SqlParameter("@Answer", SqlDbType.NVarChar, answer.Length);
            parameterAnswer.Value			= answer;
            myCommand.Parameters.Add(parameterAnswer);
			int ret							=	-1;
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
				ret			=	Convert.ToInt32(parameterItemID.Value);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB AddFAQs  moduleID:{0} userName:{1} \nex : {2}",moduleID,userName, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}

            return ret;
        }


		/// <summary>
		/// The GetFAQs function is used to get all the FAQs in the module
		/// </summary>
		public DataSet GetFAQs(int moduleID) 
		{
			SqlDataAdapter myAdapt			    = new SqlDataAdapter("dbo.adn_GetFAQs", myConnection);
            myAdapt.SelectCommand.CommandType = CommandType.StoredProcedure;

            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

            DataSet myDataSet					= new DataSet();
			try
			{
				myAdapt.Fill(myDataSet);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB GetFAQs  ex : {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
			return CheckDataSetForDates(myDataSet);
		}


		/// <summary>
		/// The GetSingleFAQs function is used to Get a single FAQ
		///	from the database for display/edit
		/// </summary>
		/// <param name="itemID">itemID</param>
		/// <returns>A SqlDataReader</returns>
		public SqlDataReader GetSingleFAQs(int itemID) 
		{
			SqlCommand myCommand = new SqlCommand("dbo.adn_GetSingleFAQs", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			SqlDataReader result			= null;
			try
			{
				myConnection.Open();
				result						= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB GetSingleFAQs itemID : {0} \nex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}


		/// <summary>
		/// The DeleteFAQs function is used to remove FAQs from the DatabaseDB
		/// </summary>
		/// <param name="itemID">itemID</param>
		/// <returns>void</returns>
		public void DeleteFAQs(int itemID)
		{
			SqlCommand myCommand = new SqlCommand("dbo.adn_DeleteFAQs", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB DeleteFAQs  itemID : {0} \nex : {1}",itemID, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}


		/// <summary>
		/// The UpdateFAQs function is used to update changes to the FAQs
		/// </summary>
		public void UpdateFAQs(int moduleID, int itemID,string userName,string question,string answer)
		{
			SqlCommand myCommand            =   new SqlCommand("dbo.adn_UpdateFAQs", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			SqlParameter parameterUserName  = new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
			parameterUserName.Value = userName;
			myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterQuestion  = new SqlParameter("@Question", SqlDbType.NVarChar, 400);
            parameterQuestion.Value         = (question.Length > 400) ? question.Substring(0,400):question;
            myCommand.Parameters.Add(parameterQuestion);

            SqlParameter parameterAnswer	= new SqlParameter("@Answer", SqlDbType.NVarChar, answer.Length);
            parameterAnswer.Value			= answer;
            myCommand.Parameters.Add(parameterAnswer);
            
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB UpdateFAQs  moduleID:{0} userName:{1} \nex : {2}",moduleID,userName, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

		public DataSet GetFAQsGlobalized(int moduleID) 
		{
			SqlDataAdapter myAdapt			= new SqlDataAdapter("dbo.adn_GetFAQsGlobalized", myConnection);
			myAdapt.SelectCommand.CommandType = CommandType.StoredProcedure;

            AddModuleIDParameters(myAdapt.SelectCommand,moduleID);

			AddGlobalizeParameters(myAdapt.SelectCommand);

			DataSet myDataSet					= new DataSet();
			try
			{
				myAdapt.Fill(myDataSet);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB GetFAQsGLobalized  ex : {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
			return CheckDataSetForDates(myDataSet);
		}

		public void UpdateFAQsGlobalized(int moduleID, int itemID,string userName,string question,string answer)
		{
			SqlCommand myCommand		        = new SqlCommand("dbo.adn_UpdateFAQsGlobalized", myConnection);
			myCommand.CommandType		        = CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

            AddItemIDParameters(myCommand,itemID);

			AddGlobalizeParameters(myCommand);

			SqlParameter parameterUserName		= new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
			parameterUserName.Value				= userName;
			myCommand.Parameters.Add(parameterUserName);

			SqlParameter parameterQuestion		= new SqlParameter("@Question", SqlDbType.NVarChar, 400);
			parameterQuestion.Value				= (question.Length > 400) ? question.Substring(0,400):question;
			myCommand.Parameters.Add(parameterQuestion);

            SqlParameter parameterAnswer	    = new SqlParameter("@Answer", SqlDbType.NVarChar, answer.Length);
            parameterAnswer.Value			    = answer;
			myCommand.Parameters.Add(parameterAnswer);
            
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB UpdateFAQsGlobalized  moduleID:{0} userName:{1} \nex : {2}",moduleID,userName, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}

		public SqlDataReader GetSingleFAQsGlobalized(int itemID) 
		{
			SqlCommand myCommand = new SqlCommand("dbo.adn_GetSingleFAQsGlobalized", myConnection);
			myCommand.CommandType = CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			AddGlobalizeParameters(myCommand);

			SqlDataReader result			= null;
			try
			{
				myConnection.Open();
				result						= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB GetSingleFAQsGlobalized itemID : {0} \nex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}
		public void DeleteFAQsGlobalized(int itemID)
		{
			SqlCommand myCommand = new SqlCommand("dbo.adn_DeleteFAQsGlobalized", myConnection);
			myCommand.CommandType = CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"FAQsDB DeleteFAQsGlobalized  itemID : {0} \nex : {1}",itemID, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
		}


	}
}
